package org.devio.secondlevelmenu.spinner;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import org.devio.secondlevelmenu.R;
import org.devio.secondlevelmenu.adapter.SaveSelectedPositionArrayAdapter;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

/**
 * Created by amazingZZ on 11/18/21
 */
public class TestSpinnerActivity extends AppCompatActivity implements NormalSpinnerAdapter.AdapterItemClick {

    private static final String[] countries = {"北京", "上海", "广州", "深圳", "成都", "杭州"};

    private TextView mTextView;
    private Spinner mSpinner;
    private ArrayAdapter<String> mAdapter;


    private TextView spinner_tv;
    private RelativeLayout spinner_tv_vp;
    private SpinnerPopupWindow mSpinerPopWindow;
    private CustomSpinner customSpinner;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_spanner);
        customSpinner = findViewById(R.id.customSpinner);
        String[] arr  = {"全部", "问题", "分享"};
        customSpinner.setStrArr(arr);


        customSpinner.setItemSelectedListener(new CustomSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(int position, String content) {
                Log.e("zz",position+content);

            }
        });


//        spinner();
//        initCustomPopWindow();
    }

    private void initCustomPopWindow() {
        spinner_tv = (TextView) findViewById(R.id.spinner_tv);
        spinner_tv_vp = (RelativeLayout) findViewById(R.id.spinner_tv_vp);
        spinner_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSpinerPopWindow.setWidth(spinner_tv_vp.getWidth());
                mSpinerPopWindow.showAsDropDown(spinner_tv_vp);
            }
        });
        String[] strArr = {"全部", "问题", "分享"};

        spinner_tv.setText(strArr[0]);
        mSpinerPopWindow = new SpinnerPopupWindow(getApplicationContext(), this);
        mSpinerPopWindow.addData(strArr);
    }


    private void spinner() {
        Spinner spinner = findViewById(R.id.spinner);
        String[] strArr = {"11", "22", "33"};
        SaveSelectedPositionArrayAdapter arrayAdapter = new SaveSelectedPositionArrayAdapter(this, R.layout.item_que_type, strArr);
        arrayAdapter.setDropDownViewResource(R.layout.item_labeldetail_spinner);
        spinner.setAdapter(arrayAdapter);
        spinner.setSelection(0);


        spinner.getBaseline();
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) view;
                tv.setTextColor(TestSpinnerActivity.this.getResources().getColor(R.color.colorPrimary));
                arrayAdapter.refreshSelectedIndex(position);

                Toast.makeText(TestSpinnerActivity.this, "" + position, Toast.LENGTH_SHORT);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        spinner.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int height = spinner.getMeasuredHeight();
                Log.e("zz", height + "");
                spinner.setDropDownVerticalOffset(height + 10);
            }
        });
    }


    @Override
    public void itemClick(int position, String string_content) {
        spinner_tv.setText(string_content);
        mSpinerPopWindow.dismiss();

    }
}
